package com.ibm.cps.newmessage;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.gson.Gson;
import com.ibm.util.JSONUtility;
import com.ibm.util.StringUtil;
import com.ibm.util.exception.CPSException;

public class RuleMetaData {
	private String rulename;
	private String rule;
	private int ruleEventMode;

	private boolean model;
	private String tenantid;
	private String ruleId;
	private String processorid;
	private String parentprocessor;
	private boolean isHit = false;
	private boolean continueHit = false;

	public RuleMetaData() {
	}
	

	public RuleMetaData(String tenantid, String ruleName, String ruleStr, String ruleEventMode, String ruleId, String processorId,
			String parentProcessors) {
		this.tenantid = tenantid;
		this.rulename = ruleName;
		this.rule = ruleStr;
		if (StringUtil.isDigit(ruleEventMode)) {
			this.ruleEventMode = Integer.parseInt(ruleEventMode);
			this.model = isEvent(this.ruleEventMode);
		} else {
			this.model = false;
		}
		this.ruleId = ruleId;
		this.processorid = processorId;
		this.parentprocessor = parentProcessors;
	}

	private boolean isEvent(int mode) {
		return mode == RuleEventType.FT || mode == RuleEventType.TF || mode == RuleEventType.FTTF
				|| mode == RuleEventType.MIX;
	}

	public String getRuleName() {
		return rulename;
	}

	public boolean isHit() {
		return isHit;
	}

	public void setHit(boolean isHit) {
		this.isHit = isHit;
	}

	public String getRule() {
		return rule;
	}

	public boolean isEventType() {
		return model;
	}

	public String getRuleString() {
		return this.rule;
	}

	public String getName() {
		return this.rulename;
	}

	public String getUserid() {
		return tenantid;
	}

	public boolean getModel() {
		return model;
	}

	public void setModel(boolean model) {
		this.model = model;
	}

	public String getRuleid() {return ruleId;}

	public String getProcessorid() {
		return processorid;
	}

	public String getParentprocessor() {
		return parentprocessor;
	}

	public int getRuleEventMode() {
		return ruleEventMode;
	}

	public void setRuleEventMode(int ruleEventMode) {
		this.ruleEventMode = ruleEventMode;
	}
	
	public boolean isContinueHit() {
		return continueHit;
	}

	public void setContinueHit(boolean continueHit) {
		this.continueHit = continueHit;
	}

	public String getRuleJsonStr() {
		Gson gson = new Gson();
		String json = gson.toJson(this);
		try {
			ObjectNode objectNode = JSONUtility.fromObjectString(json);
			objectNode.remove("type");
			objectNode.put("type", "rule");
			objectNode.remove("deletion");
			return objectNode.toString();
		} catch (CPSException e) {
			e.printStackTrace();
		}
		return null;
	}
}
